<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
//引入模型
use App\Admin\Admin;
//引入Input
use Input;
class AdminController extends Controller
{
    //展示管理员列表的方法
    public function index() {
    	//获取数据
    	$data = Admin::all();
    	//展示模板
    	return view('admin.admin.index',compact('data'));
    }

    //展示serverSide展示服务端分页
    public function serverSide($mark = '') {
    	if ($mark == 'isAjax'){
    		//接收排序的参数
    		$orderByIndex = Input::get('order.0.column');
    		$orderRule = Input::get('order.0.dir');
    		$_map = [
    			1  => 'id',
    			2  => 'username',
    			3  => 'mobile',
    			4  => 'email',
    			5  => 'role_id',
    			6  => 'created_at',
    			7  => 'status'
    		];
    		$orderByFieldName = $_map[$orderByIndex];
    		//接收分页参数
    		$start = Input::get('start'); //起始位置
    		$length = Input::get('length'); //偏移量
    		//获取关键词
    		$keyword = Input::get('search.value');
    		//ajax请求
    		return response() -> json([
					'draw'            =>  (int) Input::get('draw'),
					'recordsTotal'    =>  Admin::count(), //数据表中总的记录数
					'recordsFiltered' =>  Admin::where('username','like',"%$keyword%") -> count(),//被过滤后的记录数
					'data'            =>  Admin::orderBy($orderByFieldName,$orderRule) -> offset($start) -> limit($length) -> where('username','like',"%$keyword%") -> get() //查询到的数据
    		]);
    	} else {
    		return view('admin.admin.serverSide');
    	}
    }
}
